package com.rew.canteenbox.models;

import com.google.gson.annotations.SerializedName;

import java.util.List;

/**
 *  菜单模板.
 *
 * Created by HuiWen Ren on 2016/11/13.
 */
public class Orders {

    @SerializedName("dishes")
    private Dishes dishes;

    @SerializedName("num")
    private int num;

    public Orders(){}

    public Orders(Dishes dishes, int num){
        this.dishes = dishes;
        this.num = num;
    }

    // 添（追）加订单.
    public static void addOrder(List<Orders> ordersList, Dishes dishes) {
        int k = findDishesPosition(ordersList, dishes);
        if (k >= 0) {
            int n = ordersList.get(k).getNum();
            ordersList.get(k).setNum(n + 1);
        } else {
            ordersList.add(new Orders(dishes, 1));
        }
    }

    // 减少（取消）订单.
    public static void minusOrder(List<Orders> ordersList, Dishes dishes) {
        int k = findDishesPosition(ordersList, dishes);
        if(k != -1){
            int n = getNum(ordersList, dishes);
            if(n > 1){
                ordersList.get(k).setNum(n - 1);
            } else {
                ordersList.remove(k);
            }
        }
    }

    // 获取订单所在位置.
    public static int findDishesPosition(List<Orders> ordersList, Dishes dishes){
        for(int i = 0; i < ordersList.size(); i++){
            if(ordersList.get(i).getDishes().equals(dishes)) {
                return i;
            }
        }
        return -1;
    }

    // 获取当前订购数目.
    public static int getNum(List<Orders> ordersList, Dishes item) {
        int k = findDishesPosition(ordersList, item);
        if(k >= 0){
            return ordersList.get(findDishesPosition(ordersList, item)).getNum();
        }
        return 0;
    }

    public Dishes getDishes() {
        return dishes;
    }

    public void setDishes(Dishes dishes) {
        this.dishes = dishes;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
